package com.test.cities.controller;

import java.util.ArrayList;
import java.util.List;

import com.github.pagehelper.PageInfo;
import com.test.cities.dto.BookDTO;
import com.test.cities.po.Book;
import com.test.cities.response.BaseResponse;
import com.test.cities.response.JsonResult;
import com.test.cities.service.BookService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


@Controller
public class BookTestController{

	private static final Logger logger = LoggerFactory.getLogger(BookTestController.class);

	@Autowired
	private BookService bookService;

//	@GetMapping("/testbook.do")
//	public String indexPage(Model model) {
//		List<Book> bookList = bookService.searchAll();
//		model.addAttribute("bookList",bookList);
//		return "testbook";
//	}

//	@RequestMapping("/testbook.do/load")
//	public String loadBookList(Model model) {
//		List<Book> bookList = bookService.searchAll();
//		model.addAttribute("bookList",bookList);
//		return "testbook :: book_page";
//	}

//	@PostMapping(value = "/testbook.do/search")
//	public String search(@RequestBody BookDTO bookDTO,Model model) {
//		logger.debug("Controller查询"+bookDTO.toString());
//		List<Book> bookList = bookService.search(bookDTO);
//		model.addAttribute("bookList",bookList);
//		return "testbook :: book_page";
//	}

//	@PostMapping(value = "/testbook.do/insert")
//	public String insert(@RequestBody Book book,Model model) {
//		logger.debug("Controller保存"+book.toString());
//		bookService.insert(book);
//		List<Book> bookList = bookService.searchAll();
//		model.addAttribute("bookList",bookList);
//		return "testbook :: book_page";
//	}

	@PostMapping(value = "/testbook.do/search")
	public String search(@RequestBody BookDTO bookDTO,Model model,
						 @RequestParam(value = "pageIndex", required = false, defaultValue = "1")Integer pageIndex,
						 @RequestParam(value = "pageSize", required = false, defaultValue = "10")Integer pageSize) {
		logger.debug("Controller查询"+bookDTO.toString());
//		List<Book> bookList = bookService.search(bookDTO);
		PageInfo<Book> bookList = bookService.searchByDto(pageIndex,pageSize,bookDTO);
		model.addAttribute("page",bookList);
		model.addAttribute("path", "/testbook.do/search?pageIndex=");
		logger.debug("Controller查询"+bookList.toString());
		return "testbook :: page";
	}

//	@PostMapping(value = "/testbook.do/insert")
//	public String insert(@RequestBody Book book,Model model,
//			@RequestParam(value = "pageIndex", required = false, defaultValue = "1")Integer pageIndex,
//			@RequestParam(value = "pageSize", required = false, defaultValue = "10")Integer pageSize) {
//		logger.debug("Controller保存"+book.toString());
//		bookService.insert(book);
//		List<Book> bookList = bookService.searchAll();
//		model.addAttribute("bookList",bookList);
//		PageInfo<Book> bookList = bookService.searchAllPage(pageIndex,pageSize);
//		model.addAttribute("page",bookList);
//		model.addAttribute("path", "/testbook.do/load?pageIndex=");
//		return "testbook :: page";
//	}

	@GetMapping("/testbook.do")
	public String indexPage(Model model,
							@RequestParam(value = "pageIndex", required = false, defaultValue = "1")Integer pageIndex,
							@RequestParam(value = "pageSize", required = false, defaultValue = "10")Integer pageSize) {
		PageInfo<Book> bookList = bookService.searchAllPage(pageIndex,pageSize);
//		List<Book> bookList = bookService.searchAll();
		model.addAttribute("page",bookList);
		model.addAttribute("path", "/testbook.do/load?pageIndex=");
		return "testbook";
	}

	@RequestMapping("/testbook.do/load")
	public String loadBookList(Model model,
							   @RequestParam(value = "pageIndex", required = false, defaultValue = "1")Integer pageIndex,
							   @RequestParam(value = "pageSize", required = false, defaultValue = "10")Integer pageSize) {
		PageInfo<Book> bookList = bookService.searchAllPage(pageIndex,pageSize);
//		List<Book> bookList = bookService.searchAll();
		model.addAttribute("page",bookList);
		model.addAttribute("path", "/testbook.do/load?pageIndex=");
		return "testbook";
	}
}
